package com.ruoyi.web.controller.process;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.process.ProcessAbnormal;
import com.ruoyi.system.domain.process.ProcessInventory;
import com.ruoyi.system.service.processService.IProcessAbnormalService;
import com.ruoyi.system.service.processService.IProcessInventoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 加工异常Controller
 * 
 * @author ljx
 * @date 2020-07-06
 */
@Controller
@RequestMapping("/process/abnormal")
public class ProcessAbnormalController extends BaseController
{
    private String prefix = "process/abnormal";

    @Resource
    private IProcessAbnormalService processAbnormalService;
    @Resource
    private IProcessInventoryService iProcessInventoryService;

    @RequiresPermissions("process:abnormal:view")
    @GetMapping()
    public String abnormal()
    {
        return prefix + "/abnormal";
    }

    /**
     * 查询加工异常列表
     */
    @RequiresPermissions("process:abnormal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessAbnormal processAbnormal)
    {
        startPage();
        List<ProcessAbnormal> list = processAbnormalService.selectProcessAbnormalList(processAbnormal);
        return getDataTable(list);
    }

    /**
     * 导出加工异常列表
     */
    @RequiresPermissions("process:abnormal:export")
    @Log(title = "加工异常", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessAbnormal processAbnormal)
    {
        List<ProcessAbnormal> list = processAbnormalService.selectProcessAbnormalList(processAbnormal);
        ExcelUtil<ProcessAbnormal> util = new ExcelUtil<ProcessAbnormal>(ProcessAbnormal.class);
        return util.exportExcel(list, "abnormal");
    }

    /**
     * 新增加工异常
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存加工异常
     */
    @RequiresPermissions("process:abnormal:add")
    @Log(title = "加工异常", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessAbnormal processAbnormal)
    {
        processAbnormal.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(processAbnormalService.insertProcessAbnormal(processAbnormal));
    }

    /**
     * 修改加工异常
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ProcessAbnormal processAbnormal = processAbnormalService.selectProcessAbnormalById(id);
        mmap.put("processAbnormal", processAbnormal);
        return prefix + "/edit";
    }

    /**
     * 修改保存加工异常
     */
    @RequiresPermissions("process:abnormal:edit")
    @Log(title = "加工异常", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcessAbnormal processAbnormal)
    {
        processAbnormal.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(processAbnormalService.updateProcessAbnormal(processAbnormal));
    }

    /**
     * 删除加工异常
     */
    @RequiresPermissions("process:abnormal:remove")
    @Log(title = "加工异常", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(processAbnormalService.deleteProcessAbnormalByIds(ids));
    }

    @PostMapping( "/checkNumber")
    @ResponseBody
    public AjaxResult checkNumber(String number)
    {
        ProcessInventory inventory = new ProcessInventory();
        inventory.setNumber(number);
        List<ProcessInventory> list = iProcessInventoryService.selectProcessInventoryList(inventory);
        if (CollectionUtils.isNotEmpty(list)) {
            inventory =  list.get(0);
            return AjaxResult.success(inventory);
        }
        return AjaxResult.error("该原膜编号不存在");
    }
}
